package com.luoye.lj.util.strcheck;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 15-11-2
 * Time: 上午10:59
 * To change this template use File | Settings | File Templates.
 */
public class StrCheckUtil {
    /**
     * 获取字符串占几个字节
     * @author
     * @param str 字符串
     * @return int
     * @Date: 13-10-12
     * @Time: 下午13:02
     */
    public static int getStrByteLen(String str) {
        try {
            return str.getBytes("UTF-8").length;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return 0;
    }
    public static boolean isEmpty(String str){
        if(str==null){
            return true;
        }
        if("".equals(str)){
            return true;
        }
        return false;
    }
    /**
     * 验证字符串长度是否在合法区间内
     * @author
     * @param str 字符串
     * @param minLen:int 最小长度
     * @param maxLen:int 最大长度
     * @return boolean
     * @Date: 13-10-12
     * @Time: 上午11:26
     */
    public static boolean chkStrLen(String str, int minLen, int maxLen) {
        boolean flag = false;
        int strLen =getStrByteLen(str);
        if (strLen >= minLen && strLen <= maxLen) {
            flag = true;
        }
        return flag;
    }

    /**
     * 验证字符格式是否正确
     * @author
     * @param str 字符串
     * @param type 类型
     * @return boolean
     * @Date: 13-10-12
     * @Time: 下午17:01
     */
    public static boolean chkStrFormat(String str, String type) {      //输入数据正则判断
        String regex = "[\\u4e00-\\u9fa5|A-Za-z]+\\w*";
        if ("name".equals(type))    {regex = "[\\u4e00-\\u9fa5|A-Za-z|0-9]+\\w*";}
        if ("pwd".equals(type))     {regex = "[A-Za-z|\\w]{6,20}";}
        if ("email".equals(type))   {regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";}
        //if (type=="passport"){regex = "\\d{15}|\\d{18}"};
        if ("passport".equals(type)){regex = "([1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3})|([1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X))";}
        if ("mobile".equals(type))  {regex = "((13)|(14)|(15)|(17)|(18))\\d{9}";}
        if ("money".equals(type))   {regex = "([1-9]\\d*(\\.\\d{1,2})?$)|(^[0]{1}(\\.\\d{1,2})?)";}
        if ("phone".equals(type))   {regex = "(((13)|(14)|(15)|(17)|(18))\\d{9}$)|(^(\\d{3,4}|\\d{3,4}(-)?)?\\d{7,8})";}
        if("IDCard".equals(type)){regex="(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";}

        Pattern pattern = Pattern.compile(regex);
        //通过Pattern获得Matcher
        Matcher matcher = pattern.matcher(str);
        //判断用户输入是否为身份证号
        if(matcher.matches()){
            return true;
        }
        return false;
    }

    /**
     *校验邮箱格式
     */
    public static boolean checkEmail(String value){
        boolean flag=false;
        Pattern p1 = null;
        Matcher m = null;
        p1 = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
        m = p1.matcher(value);
        flag = m.matches();
        return flag;
    }
    /**
     * @param checkType 校验类型：0校验手机号码，1校验座机号码，2两者都校验满足其一就可
     * @param phoneNum
     * */
    public static boolean validPhoneNum(String checkType,String phoneNum){
        boolean flag=false;
        Pattern p1 = null;
        Pattern p2 = null;
        Matcher m = null;
        p1 = Pattern.compile("^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\\d{8})?$");
        p2 = Pattern.compile("^(0[0-9]{2,3}\\-)?([1-9][0-9]{6,7})$");
        if("0".equals(checkType)){
            System.out.println(phoneNum.length());
            if(phoneNum.length()!=11){
                return false;
            }else{
                m = p1.matcher(phoneNum);
                flag = m.matches();
            }
        }else if("1".equals(checkType)){
            if(phoneNum.length()<11||phoneNum.length()>=16){
                return false;
            }else{
                m = p2.matcher(phoneNum);
                flag = m.matches();
            }
        }else if("2".equals(checkType)){
            if(!((phoneNum.length() == 11 && p1.matcher(phoneNum).matches())||(phoneNum.length()<16&&p2.matcher(phoneNum).matches()))){
                return false;
            }else{
                flag = true;
            }
        }
        return flag;
    }
}
